package cn.yusei.teamTribe.controller.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.dao.user.po.UserInfoPO;
import cn.yusei.teamTribe.entity.Student;
import cn.yusei.teamTribe.entity.ValidateCode;
import cn.yusei.teamTribe.service.user.UserInfoService;
import cn.yusei.teamTribe.spider.Spider;
import cn.yusei.teamTribe.spider.SpiderManager;
import cn.yusei.teamTribe.spider.exception.AccessException;
import cn.yusei.teamTribe.spider.exception.UnknownException;
import cn.yusei.teamTribe.spider.exception.ValidateCodeErrorException;
import cn.yusei.teamTribe.util.ResponseUtil;
import cn.yusei.teamTribe.util.StudentUtil;

@Controller
public class NoAuthController {
	
	@Autowired
	private UserInfoService userInfoService;
	
	private static Map<Integer, Spider> spiderPool = new HashMap<Integer, Spider>();
	
	private Spider getSpider(int schoolId)
	{
		Spider spider =spiderPool.get(schoolId); 
		if(spider == null)
		{
			spider = SpiderManager.get(schoolId);
			if(spider != null)
				spiderPool.put(schoolId, spider);
		}
		return spider;
	}

	/**
	 * 
	 * @param id
	 * @param schoolId
	 * @param out 返回教务网站的验证码 (通过内容) 及sessionId (通过Cookie中的'sessionId')
	 */
	@RequestMapping(value="/user/noAuth/getValidateCode")
	public void getValidateCode(@RequestAttribute("id") int id, @RequestParam("schoolId") int schoolId, HttpServletResponse out)
	{
		Spider spider = getSpider(schoolId);
		if(spider == null)
			return;
		ValidateCode validateCode  = null;
		try
		{
			validateCode = spider.getValidateCode();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return;
		}
		out.addCookie(new Cookie("sessionId", validateCode.getSessionId()));
		ResponseUtil.responsePicture(out, validateCode.getData());
	}
	
	/**
	 * 
	 * @param id
	 * @param schoolId
	 * @param no		学号
	 * @param password	处理后的教务网密码，长沙理工大学是MD5之后的长度为32的字符
	 * @param validateCode	输入的验证码
	 * @param sessionId	教务网验证码对应的sessionId
	 * @param out
	 */
	@RequestMapping(value="/user/noAuth/auth", method=RequestMethod.POST)
	public void auth(
			@RequestAttribute("id") int id,
			@RequestParam("schoolId") int schoolId,
			@RequestParam("no") String no,
			@RequestParam("password") String password,
			@RequestParam("validateCode") String validateCode,
			@RequestParam("sessionId") String sessionId, 
			PrintWriter out)
	{
		Spider spider = getSpider(schoolId);
		if(spider == null)
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code;
		try
		{
			Student student = spider.getStudent(no, password, validateCode, sessionId);
			UserInfoPO userInfo = new UserInfoPO();
			StudentUtil.studentToPO(student, userInfo.getStudentPO());
			userInfo.setId(id);
			code = userInfoService.updateAuth(userInfo);
		}
		catch (AccessException e)
		{
			ResponseUtil.responseCode(out, ResponseCode.EDU_WEBSITE_ACCESS_ERROR);
			return;
		}
		catch (ValidateCodeErrorException e)
		{
			ResponseUtil.responseCode(out, ResponseCode.EDU_WEBSITE_VALIDATECODE_ERROR);
			return;
		}
		catch (UnknownException e)
		{
			ResponseUtil.responseCode(out, ResponseCode.EDU_WEBSITE_UNKNOWN_ERROR);
			return;
		}
		catch (IOException e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.EDU_WEBSITE_UNACCESSIBALE);
			return;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, code);
	}
}
